﻿using MaiPlanServer.IServices.IEquipmentServices;
using MaiPlanServer.Model;
using MaiPlanServer.Utility;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MaiPlanServer.API.Controllers.Equipment
{
    [Route("api/[controller]")]
    [ApiController]
    public class RepairOrderController : ControllerBase
    {

        public RepairOrderController(IRepairOrderServices RoDb)
        {
            this.RoDb = RoDb;
        }
        public IRepairOrderServices RoDb { get; }

        /// <summary>
        /// 查询设备维修单
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="repair_code"></param>
        /// <param name="repair_name"></param>
        /// <param name="machinery_code"></param>
        /// <param name="machinery_name"></param>
        /// <param name="repair_result"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get(int PageIndex, int PageSize, string repair_code, string repair_name, string machinery_code, string machinery_name, string repair_result, string status)
        {
            try
            {

                var list = await RoDb.GetJsonAsync( PageIndex,PageSize,repair_code,repair_name,machinery_code,machinery_name,repair_result,status);
                var count = await RoDb.GetCountAsync(repair_name);

                return Ok(BaseState.CreateInstance(200, "请求成功", list, count));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求失败ex"));
            }
        }

        [HttpGet("GetLine")]
        public async Task<IActionResult> GetLine(string repair_id)
        {
            try
            {
                var list = await RoDb.GetLine(repair_id);
                return Ok(BaseState.CreateInstance(200, "请求成功", list));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求失败ex"));
            }
        }

        /// <summary>
        /// 添加维修单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add([FromForm] dv_repair model)
        {
            try
            {
                if (!string.IsNullOrEmpty(model.machinery_name))
                {
                    model.repair_id = Guid.NewGuid().ToString();
                    model.create_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    model.repair_code = await RoDb.GetCodeasync();
                    var add = await RoDb.AddAsync(model);

                    return Ok(new { code = 200, msg = "请求成功" });
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return Ok(new { code = -1, msg = "请求失败", count = 0, data = "" });
        }
        /// <summary>
        /// 添加步骤
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost("Adds")]
        public async Task<IActionResult> Adds([FromForm] dv_repair_line model)
        {
            try
            {
                if (!string.IsNullOrEmpty(model.subject_name))
                {
                    model.line_id = Guid.NewGuid().ToString();
                    model.subject_id = Guid.NewGuid().ToString();
                    model.create_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    var add = await RoDb.Adds(model);

                    return Ok(new { code = 200, msg = "请求成功" });
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return Ok(new { code = -1, msg = "请求失败", count = 0, data = "" });
        }

        /// <summary>
        /// 修改维修单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> Put([FromForm] dv_repair model)
        {
            try
            {
                var result = await RoDb.UpdateAsync(model);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "修改成功"));
                }
            }
            catch (System.Exception ex)
            {
                //后续写日志
            }
            return Ok(BaseState.CreateInstance(-1, "修改失败，服务器异常"));
        }

        /// <summary>
        /// 删除维修单
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> Del(string Id)
        {
            try
            {
                var model = await RoDb.DellAsync(Id);
                if (model)
                {
                    return Ok(BaseState.CreateInstance(200, "删除成功"));
                }
                return Ok(BaseState.CreateInstance(-1, "删除失败"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(-1, "删除失败"));
            }
        }


        /// <summary>
        /// 删除多条维修单
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete("DelAll")]
        public async Task<IActionResult> DelAll(string Id)
        {
            try
            {
                string[] Ids = Id.Split(',');
                var model = await RoDb.DellAllAsync(Ids);
                if (model)
                {
                    return Ok(BaseState.CreateInstance(200, "删除成功"));
                }
                return Ok(BaseState.CreateInstance(-1, "删除失败"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(-1, "删除失败"));
            }
        }

    }
}
